package com.example.http;

import com.example.util.UUIDUtil;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class HttpClient {

    public static void start(int port, String uri, String host) throws IOException {
        System.out.println("开始启动HTTP服务...");

        HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
        server.createContext(uri, (HttpExchange exchange) -> {
            String response = UUIDUtil.generateUUID();
            exchange.sendResponseHeaders(200, 0);
            OutputStream os = exchange.getResponseBody();
            os.write(response.getBytes());
            os.close();
        });
        server.start();

        System.out.println("HTTP服务启动成功.http://" + host + ":" + port + uri);
    }

}
